package com.cc.web;

import com.cc.enums.CommonEnum;
import com.cc.exception.CommonException;
import com.cc.pojo.User;
import com.cc.result.ResultBody;
import com.cc.service.ExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;



/**
 * 控制层： 测试自定义异常、空指针异常、未知异常
 */
@RestController
@RequestMapping(value = "/api")
public class UserRestController {

    @Autowired
    ExceptionService exceptionService;

    /**
     * 请求参数：{"id":1,"age":18}
     * @param user
     * @return
     */
	@PostMapping("/user")
    public boolean insert(@RequestBody User user) {
    	//测试 业务自定义异常
        if(user.getName()==null){
            throw  new CommonException(CommonEnum.PARAMETER_FAILED);
        }
        return true;
    }

    /**
     * 请求参数：{"id":1,"age":18}
     * @param user
     * @return
     */
    @PutMapping("/user")
    public boolean update(@RequestBody User user) {
        //测试 业务层中发生空指针异常
        boolean flg = exceptionService.serviceTest();
        return true;
    }

    /**
     * 请求参数：{"id":1,"age":18}
     * @param user
     * @return
     */
    @DeleteMapping("/user")
    public boolean delete(@RequestBody User user)  {
        //内部服务器异常：未知异常
        boolean flg = exceptionService.serviceTest2();
        return true;
    }

    /**
     *  不需要参数
     * @param user
     * @return
     */
    @GetMapping("/user")
    @ResponseBody
    public ResultBody findByUser(User user) {
    	System.out.println("正常查询用户，没有异常。");
        List<User> userList =new ArrayList<>();
        User user2=new User();
        user2.setId(1L);
        user2.setName("cc");
        user2.setAge(18);
        userList.add(user2);
        return ResultBody.success(userList);
    }
    
}
